package com.nineclock.system.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.constant.SMSConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.sms.AliyunSmsUtils;
import com.nineclock.common.utils.PhoneNumCheckUtils;

import com.nineclock.system.mapper.SysUserMapper;
import com.nineclock.system.pojo.SysUser;
import com.nineclock.system.service.SMSService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class SMSServiceImpl implements SMSService {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AliyunSmsUtils aliyunSmsUtils;

    /*
     * 发送验证码
     * */
    @Override
    public void sendCode(String mobile, String type) {
        //判断数据是否为空
        if (StrUtil.isEmpty(mobile) || StrUtil.isEmpty(type)) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        //判断是否手机号是否符合规则
        if (!PhoneNumCheckUtils.isPhoneLegal(mobile)) {
            throw new NcException(ResponseEnum.INVALID_PHONE_NUMBER);
        }

        //判读当前手机号是否可用
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getMobile, mobile);
        SysUser sysUser = userMapper.selectOne(wrapper);
        if (type.equals("2")) {
            if (sysUser != null) {
                throw new NcException(ResponseEnum.USER_MOBILE_EXISTS);
            }
            }else {
            if (sysUser == null) {
                throw new NcException(ResponseEnum.USER_NOT_REGISTER);
            }
        }

        //生成验证码
        String code = RandomUtil.randomNumbers(6);
        //发送验证码到手机
        aliyunSmsUtils.sendSMS(mobile, code);


        //存储验证码到Redis
        String key = getPrefix(type) + mobile;
        redisTemplate.boundValueOps(key).set(code, 5, TimeUnit.MINUTES);

    }

    /*
            验证码校验
    */
    @Override
    public Boolean verifyCode(String checkcode, String mobile, String type) {
        //判断数据是否为空
        if (StrUtil.isEmpty(checkcode) || StrUtil.isEmpty(mobile) || StrUtil.isEmpty(type)) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        //根据手机号从redis中获取去验证码进行对比
        String prefix = this.getPrefix(type);
        String realCode = (String) redisTemplate.boundValueOps(prefix + mobile).get();

        if (StrUtil.isNotEmpty(realCode) || realCode.equals(checkcode)) {
            return true;
        }


        return false;
    }

    //删除redis中的验证码

    @Override
    public void deleteSmsCode(String mobile, String type) {
        String redisKey = this.getPrefix(type) + mobile;

        redisTemplate.delete(redisKey);
    }

    //前缀
    public String getPrefix(String type) {
        switch (type) {
            case SMSConstant.SMS_TYPE_LOGIN:
                return SMSConstant.SMS_LOGIN_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_REGISTER:
                return SMSConstant.SMS_REGISTER_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_CHANGE_MANAGER:
                return SMSConstant.SMS_CHANGE_MANAGER_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_RESET_PASSWORD:
                return SMSConstant.SMS_RESET_KEY_PREFIX;
            default:
                return null;
        }
    }
}
